package io.github.topcatv.mall.common.handler;

import io.github.topcatv.mall.common.ao.ReturnObject;
import io.github.topcatv.mall.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liuyi
 */
@ControllerAdvice
@Slf4j
public class ZpmExceptionHandler {

    /**
     * 统一异常处理方法
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ReturnObject handleException(Exception exception, HttpServletRequest request, HttpServletResponse response) {
        String msg = "服务器有些问题";
        if (exception instanceof BizException) {
            msg = exception.getMessage();
        }
        if (exception instanceof CredentialsExpiredException) {
            return ReturnObject.forbidden();
        }
        log.warn("", exception);
        return ReturnObject.error(msg);
    }
}
